﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HexEditor_
{
    public class DataTypeManager
    {
        Dictionary<string, DataType> _AvailableDataTypes;
        public List<string> _AvailableTypeList { get; set; }
        public string DefaultDataType{get;set;}


        public void Initialize()
        {
            if (_AvailableDataTypes == null)
            {
                _AvailableDataTypes = new Dictionary<string, DataType>();
                _AvailableTypeList = new List<string>();
                AddType("char", new DataTypeChar());
                AddType("short", new DataTypeShort());
                AddType("ushort", new DataTypeUshort());
                AddType("int", new DataTypeInt());
                AddType("uint", new DataTypeUint());
                AddType("uint64", new DataTypeUint64());
                AddType("int64", new DataTypeInt64());
                AddType("float", new DataTypeFloat());
                AddType("double", new DataTypeDouble());
            }

            DefaultDataType = "char";
        }

        public void AddType(string name, DataType dt)
        {
            _AvailableDataTypes[name] = dt;
            _AvailableTypeList.Add(name);
        }

        public DataType GetDataType(string typeName)
        {
            typeName = typeName.ToLower();
            if (_AvailableDataTypes.ContainsKey(typeName))
            {
                return _AvailableDataTypes[typeName];
            }
            return null;
        }

        public int Parse(byte[] bytes, int offset,
            string typeName, out string res)
        {
            DataType dt = GetDataType(typeName);
            if (dt != null)
            {
                return dt.Parse(bytes, offset, out res);
            }
            res = "";
            return 0;
        }

        public bool IsExist(string name)
        {
            return _AvailableDataTypes.ContainsKey(name);
        }
    }
}
